package com.applink.sell_proof.database.table;

import java.util.ArrayList;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;

import com.applink.sell_proof.EMarketing;
import com.applink.sell_proof.database.EMarketingContentProvider;
import com.applink.sell_proof.model.UserSetting;

public class UserSettingDBHelper {
	public static final String TABLE_NAME = "user_setting_table";

	public static final String USER_ID = "UserId";
	public static final String USER_WEB_LOGIN = "WebLogin";
	public static final String USER_SALUTATION = "Salutation";
	public static final String USER_FIRST_NAME = "FirstName";
	public static final String USER_LAST_NAME = "LastName";
	public static final String USER_MOBILE1 = "Mobile1";
	public static final String USER_EMAIL1 = "Email1";
	public static final String USER_JOB = "JobTitle";
	public static final String USER_PARTNER_COMPANY_NAME = "PartnerCompName";
	public static final String USER_COUNTRY = "Country";
	public static final String USER_COM_ADD_1 = "PartnerCompAddress1";
	public static final String USER_COM_ADD_2 = "PartnerCompAddress2";
	public static final String USER_COM_ADD_3 = "PartnerCompAddress3";
	public static final String USER_COM_POST_CODE = "PartnerCompPostCode";
	public static final String USER_COM_PHONE = "PartnerCompOfficeNo1";
	public static final String USER_COM_FAX = "PartnerCompFax";

	public static final String SQL_EXCUTE = "CREATE TABLE " + TABLE_NAME + "("
			+ USER_ID + " integer primary key ," + USER_WEB_LOGIN + " text ,"
			+ USER_SALUTATION + " text ," + USER_FIRST_NAME
			+ " text ," + USER_LAST_NAME
			+ " text ," + USER_MOBILE1 
			+ " text ," + USER_EMAIL1
			+ " text ," + USER_JOB
			+ " text ," + USER_PARTNER_COMPANY_NAME 
			+ " text ," + USER_COUNTRY
			+ " text ," + USER_COM_ADD_1 
			+ " text ," + USER_COM_ADD_2
			+ " text ," + USER_COM_ADD_3 
			+ " text ," + USER_COM_POST_CODE
			+ " text ," + USER_COM_PHONE 
			+ " text ," + USER_COM_FAX 
			+ " text " + ");";
	
	public static void createUserSetting(final UserSetting userSetting) {
		new Thread() {
			@Override
			public void run() {
				super.run();
				ContentValues value = new ContentValues();
				ContentResolver resolver = EMarketing.getInstance()
						.getApplicationContext().getContentResolver();
				value.put(USER_ID, userSetting.getUserId());
				value.put(USER_WEB_LOGIN, userSetting.getWebLogin());
				value.put(USER_SALUTATION, userSetting.getSalutation());
				value.put(USER_FIRST_NAME, userSetting.getFirstName());
				value.put(USER_LAST_NAME, userSetting.getLastName());
				value.put(USER_MOBILE1, userSetting.getMobile1());
				value.put(USER_EMAIL1, userSetting.getEmail1());
				value.put(USER_JOB, userSetting.getJobTitle());
				value.put(USER_PARTNER_COMPANY_NAME, userSetting.getPartnerCompName());
				value.put(USER_COUNTRY, userSetting.getCountry());
				value.put(USER_COM_ADD_1, userSetting.getPartnerCompAddress1());
				value.put(USER_COM_ADD_2, userSetting.getPartnerCompAddress2());
				value.put(USER_COM_ADD_3, userSetting.getPartnerCompAddress3());
				value.put(USER_COM_POST_CODE, userSetting.getPartnerCompPostCode());
				value.put(USER_COM_PHONE, userSetting.getPartnerCompOfficeNo1());
				value.put(USER_COM_FAX, userSetting.getPartnerCompFax());
				resolver.insert(EMarketingContentProvider.USER_SETTING_CONTENT_URI,
						value);
			}

		}.start();
	}
	
	public static ArrayList<UserSetting> getAllUserSetting() {
		ContentResolver resolver = EMarketing.getInstance()
				.getApplicationContext().getContentResolver();
		Cursor cursor = resolver.query(
				EMarketingContentProvider.USER_SETTING_CONTENT_URI, null, null,
				null, null);

		if (cursor != null) {
			ArrayList<UserSetting> userSettings = new ArrayList<UserSetting>();
			while (cursor.moveToNext()) {
				UserSetting userSetting = new UserSetting();
				userSetting.setUserId(cursor.getInt(cursor.getColumnIndex(USER_ID)));
				userSetting.setWebLogin(cursor.getString(cursor.getColumnIndex(USER_WEB_LOGIN)));
				userSetting.setSalutation(cursor.getString(cursor.getColumnIndex(USER_SALUTATION)));
				userSetting.setFirstName(cursor.getString(cursor.getColumnIndex(USER_FIRST_NAME)));
				userSetting.setLastName(cursor.getString(cursor.getColumnIndex(USER_LAST_NAME)));
				userSetting.setMobile1(cursor.getString(cursor.getColumnIndex(USER_MOBILE1)));
				userSetting.setEmail1(cursor.getString(cursor.getColumnIndex(USER_EMAIL1)));
				userSetting.setJobTitle(cursor.getString(cursor.getColumnIndex(USER_JOB)));
				userSetting.setPartnerCompName(cursor.getString(cursor.getColumnIndex(USER_PARTNER_COMPANY_NAME)));
				userSetting.setCountry(cursor.getString(cursor.getColumnIndex(USER_COUNTRY)));
				userSetting.setPartnerCompAddress1(cursor.getString(cursor.getColumnIndex(USER_COM_ADD_1)));
				userSetting.setPartnerCompAddress2(cursor.getString(cursor.getColumnIndex(USER_COM_ADD_2)));
				userSetting.setPartnerCompAddress3(cursor.getString(cursor.getColumnIndex(USER_COM_ADD_3)));
				userSetting.setPartnerCompPostCode(cursor.getString(cursor.getColumnIndex(USER_COM_POST_CODE)));
				userSetting.setPartnerCompOfficeNo1(cursor.getString(cursor.getColumnIndex(USER_COM_PHONE)));
				userSetting.setPartnerCompFax(cursor.getString(cursor.getColumnIndex(USER_COM_FAX)));
				userSettings.add(userSetting);
			}
			return userSettings.size() > 0 ? userSettings : null;
		}
		return null;
	}
}
